package com.ftl.autopunch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ftl.autopunch.entity.SysLog;
import com.ftl.autopunch.entity.SysUser;
import com.ftl.autopunch.model.LoginFormModel;
import com.ftl.autopunch.model.UserTokenModel;
import com.ftl.autopunch.service.PunchService;
import com.ftl.autopunch.service.SysLogService;
import com.ftl.autopunch.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/punch")
public class PunchController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysLogService sysLogService;
    @Autowired
    PunchService punchService;

    public boolean doPunch(SysUser sysUser) throws IOException {
        LoginFormModel loginFormModel = sysUserService.getLoginForm();
        UserTokenModel userTokenModel = sysUserService.doLogin(loginFormModel, sysUser);
        boolean punchStatus = punchService.punchAction(userTokenModel.getServerSessionId(), userTokenModel.getUserSessionId());
        return punchStatus;
    }

    //单用户签到
    @GetMapping("/one")
    public R punchOne(@RequestParam Integer userId) {
        SysUser sysUser = sysUserService.getById(userId);
        try {
            boolean punchStatus = doPunch(sysUser);
            if (punchStatus) {
                sysUser.setStatus(1);
                sysUserService.saveOrUpdate(sysUser);
                return R.ok("签到成功");
            } else throw new Exception("签到失败");
        } catch (Exception e) {
            e.printStackTrace();
            sysUser.setStatus(0);
            sysUserService.saveOrUpdate(sysUser);
            SysLog sysLog = new SysLog();
            sysLog.setUserId(userId);
            sysLog.setUserName(sysUserService.getById(userId).getUsername());
            sysLog.setStatus(0);
            sysLogService.save(sysLog);
            return R.failed("签到失败");
        }
    }

    //全部签到
    @GetMapping("/all")
    public synchronized R punchAll() throws IOException, InterruptedException {
        List<SysUser> sysUserList = sysUserService.list();
        for (SysUser sysUser : sysUserList) {
            boolean punchStatus = doPunch(sysUser);
            Thread.sleep(3000);
            SysLog sysLog = new SysLog();
            sysLog.setUserId(sysUser.getId());
            sysLog.setUserName(sysUser.getUsername());
            if (punchStatus) {
                sysUser.setStatus(1);
                sysLog.setStatus(1);
            } else {
                sysUser.setStatus(0);
                sysLog.setStatus(0);
            }
            sysUserService.saveOrUpdate(sysUser);
            sysLogService.save(sysLog);
        }
        return R.ok("签到完毕");
    }

    //一键补签
    @GetMapping("/retry")
    public synchronized R retry() throws IOException, InterruptedException {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long zero = calendar.getTimeInMillis();
        queryWrapper.between("last_check", zero, System.currentTimeMillis());
        List<SysUser> sysUsers = sysUserService.list(queryWrapper);
        for (SysUser sysUser : sysUsers) {
            boolean punchStatus = doPunch(sysUser);
            Thread.sleep(3000);
            SysLog sysLog = new SysLog();
            sysLog.setUserId(sysUser.getId());
            sysLog.setUserName(sysUser.getUsername());
            if (punchStatus) {
                sysUser.setStatus(1);
                sysLog.setStatus(1);
            } else {
                sysUser.setStatus(0);
                sysLog.setStatus(0);
            }
            sysUserService.saveOrUpdate(sysUser);
            sysLogService.save(sysLog);
        }
        return R.ok("补签成功");
    }
}
